package com.sc.nft.controller;

import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.entity.StaticData;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.ecology.*;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.EcologyHatchExchangeConfService;
import com.sc.nft.service.EcologySmeltingService;
import com.sc.nft.service.UserCreatorInfoService;
import com.sc.nft.service.UserEcologyHatchUpperLimitDetailsService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ecology/hatch")
@Api(tags = "生态熔炼")
public class EcologySmeltingController {

    private final EcologySmeltingService ecologySmeltingService;
    private final Redisson redisson;

    private final EcologyHatchExchangeConfService ecologyHatchExchangeConfService;

    private final UserEcologyHatchUpperLimitDetailsService userEcologyHatchUpperLimitDetailsService;
    private final UserCreatorInfoService userCreatorInfoService;


    @ApiOperation(value = "生态孵化主页", notes = "生态孵化主页", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/home", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<HatchHomeVO> hatchHome() {
        return Result.success(ecologySmeltingService.hatchHome(UserPool.getUser()));
    }

    @ApiOperation(value = "熔炼列表", notes = "熔炼列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hatchPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<HatchPageVO> hatchPage(@Valid @RequestBody SaasIdPageReq request) {
        return PageResult.success(ecologySmeltingService.hatchPage(UserPool.getUser(), request.getPageNo(), request.getPageSize(),request.getUserCreatorId()));
    }

    @ApiOperation(value = "获取熔炼剩余次数", notes = "获取熔炼剩余次数", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getHatchCount", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<HatchCountVO> getHatchCount(@Valid @RequestBody IdRequest request) {
        return Result.success(ecologySmeltingService.getHatchCount(request.getId(), UserPool.getUser()));
    }

    @ApiOperation(value = "预获取熔炼奖励", notes = "熔炼列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/preHatchRewards", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<HatchRewardsSummaryVO> preHatchRewards(@Valid @RequestBody HatchRewardsRequest request) {
        return Result.success(ecologySmeltingService.preHatchRewards(request.getId(), request.getMethodId(), request.getHatchCount(), UserPool.getUser()));
    }


    @ApiOperation(value = "熔炼", notes = "熔炼", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hatch", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result hatch(@Valid @RequestBody HatchRequest request) throws GlobalException {

        UserInfo user = UserPool.getUser();

        if (!StrUtil.equals(request.getPayPwd(), user.getTransactionPassword())) {
            return Result.fail(ErrorCode.PASSWORD_ERROR);
        }

        RLock lock = redisson.getLock(RedisKeyEnum.HATCH_LOCK.getKey() + user.getId());
        try {
            boolean res = lock.tryLock(10, TimeUnit.SECONDS);
            if (res) {
                ecologySmeltingService.hatch(request.getId(), request.getMethodId(), request.getHatchCount(), user);
            } else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalException("系统繁忙,请稍后重试!");
        } finally {
            lock.unlock();
        }

        return Result.success();
    }

    @ApiOperation(value = "熔炼记录", notes = "熔炼记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/pageHatchRecords", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<PageHatchRecordsVO> pageHatchRecords(@Valid @RequestBody PageRequest request) {
        return PageResult.success(ecologySmeltingService.pageHatchRecords(request.getPageNo(), request.getPageSize(), UserPool.getUser()));
    }

    @ApiOperation(value = "熔炼记录详情", notes = "熔炼记录详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/hatchRecordsDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<HatchRecordsDetailsVO> hatchRecordsDetails(@Valid @RequestBody IdRequest request) {
        return Result.success(ecologySmeltingService.hatchRecordsDetails(UserPool.getUser(), request.getId()));
    }


    @ApiOperation(value = "我的船员", notes = "我的船员", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myRrew", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MyRrewVO> myRrew() {
        return Result.success(ecologySmeltingService.myRrew(UserPool.getUser()));
    }

    @ApiOperation(value = "我的船员", notes = "我的船员", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myJunioRecologyHatchRecords", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<JunioRecologyHatchRecords> myJunioRecologyHatchRecords(@Valid @RequestBody PageRequest request) {
        return PageResult.success(ecologySmeltingService.myJunioRecologyHatchRecords(UserPool.getUser(), request.getPageNo(), request.getPageSize()));
    }


    @ApiOperation(value = "我的燃料详情", notes = "我的燃料详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myPointsDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MyPointsDetailsVO> myPointsDetails() {
        return Result.success(ecologySmeltingService.myPointsDetails(UserPool.getUser()));
    }

    @ApiOperation(value = "我的燃料记录", notes = "我的燃料记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myPointsRecords", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyPointsRecordsVO> myPointsRecords(@Valid @RequestBody MyPointsRecordsRequest request) {
        return PageResult.success(ecologySmeltingService.myPointsRecords(UserPool.getUser(), request.getType(), request.getPageNo(), request.getPageSize()));
    }


    @ApiOperation(value = "可释放额度记录", notes = "可释放额度记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userEcologyUsableUpperLimitPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<UserEcologyUsableUpperLimitPageVO> userEcologyUsableUpperLimitPage(@Valid @RequestBody PageRequest request) {
        return PageResult.success(ecologySmeltingService.userEcologyUsableUpperLimitPage(UserPool.getUser(), request.getPageNo(), request.getPageSize()));
    }


    @ApiOperation(value = "saas是否开启展示生态熔炼", notes = "更新是否开启saas展示生态熔炼", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/updateShowEcologyStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<Boolean> getShowEcologyStatus(@RequestBody @Valid IdRequest request) {
        return Result.success(userCreatorInfoService.getShowEcologyStatus(request.getId()));
    }

}
